#include<iostream>
using namespace std;
const int N = 1e5 + 10;
int p[N];
int arr[N];
int n, m;
int find(int x)
{
	if (p[x] != x) {
		p[x] = find(p[x]);
	}
	return p[x];
}
int main()
{
	cin >> n >> m;
	for (int i = 1; i < N; i++) {
		p[i] = i;
	}
	for (int i = 0; i < n; i++) {
		cin >> arr[i];
	}
	while (m--) {
		int a, b;
		cin >> a >> b;
		a = find(a);
		b = find(b);
		if (a != b) {
			p[a] = b;
		}
	}
	for (int i = 0; i < n; i++) {
		cout << find(arr[i]) << " ";
	}
	return 0;
}